package org.example.test.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.test.entity.Dept;
import org.example.test.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;
    @RequestMapping("/getdept")
    public Map<String, Object> getdept(Integer pageNum, Integer pageSize, String dname) {
        //既存放数据，又存放总条数
        Map<String, Object> map = new HashMap<String, Object>();
        Page<Object> startPage = PageHelper.startPage(pageNum, pageSize);
        map.put("deptList", deptService.findAll(dname));//向容器中放获取到的数据
        map.put("total", startPage.getTotal());//向容器中放数据总条数
        return map;
    }

    @RequestMapping("/delete")
    public int delete(Integer deptno){
        return deptService.delete(deptno);
    }
    @RequestMapping("/update")
    public int editDept(@RequestBody Dept dept){
        return deptService.editDept(dept);
    }
    @RequestMapping("/addDept")
    public int addDept(@RequestBody Dept dept){
        return deptService.addDept(dept);
    }
}
